<template>
    <div class="container">
        <div id="Home_page">
            <div style="cursor: pointer;" @click="goBack"><span id="back_home" class="iconfont icon-fanhui">回到首页</span>
            </div>
            <h3>修改个人信息</h3>
        </div>
        <div class="center">
            <el-form :model="form" id="form" label-width="auto" :rules="rules" ref="change_form"
                style="max-width: 600px;" label-position="top">
                <el-form-item label="用户名">
                    <el-input v-model="form.username" />
                </el-form-item>
                <el-form-item label="密码">
                    <el-input v-model="form.password" placeholder="修改密码，不修改留空" />
                </el-form-item>
                <el-form-item label="手机号" prop="phone">
                    <el-input v-model="form.phone" />
                </el-form-item>
                <el-form-item label="身份证号" prop="number">
                    <el-input v-model="form.number" />
                </el-form-item>
                <el-form-item label="年龄">
                    <el-input v-model="form.age" type="number" />
                </el-form-item>
                <el-form-item label="父母">
                    <el-input v-model="form.parents" />
                </el-form-item>
                <el-form-item label="地址">
                    <el-input v-model="form.address" />
                </el-form-item>
            </el-form>
            <el-button type="primary" style="width: 200px;" @click="onSubmit" size="large">提交信息</el-button>
        </div>
    </div>

</template>

<script setup>
import * as qiniu from 'qiniu-js'
import { ref, onMounted, reactive } from "vue"
import axios from '../utils/http.js'
import { useRouter } from 'vue-router'
let router = useRouter()
import { ElMessage } from "element-plus";
const goBack = () => {
    router.back(-1)
}
let change_form = ref(null)
function update() {
    axios({
        url: 'user/edit_oneself/',
        method: 'GET',
        params: {
            username: localStorage.getItem('username')
        }
    }).then(res => {
        let data = res.data.data
        form.username = data.username
        // form.password = data.password
        form.phone = data.phone
        if (res.data.code == 200) {
            form.number = data.number
            form.age = data.age
            form.sex = data.sex
            form.parents = data.parents
            form.address = data.address
        }
        ElMessage({
            message: "获取信息成功，留空为暂无个人信息",
            type: "success",
        })
    })
}
onMounted(() => {
    update()
})
const form = reactive({
    username: '',
    password: '',
    phone: '',
    number: '',
    age: '',
    parents: '',
    address: ''
})
const phone = (rule, value, callback) => {
    if (value) {
        setTimeout(() => {
            let p = /^1[3-9]\d{9}$/;
            if (!p.test(value)) {
                callback(new Error('请输入正确的11位手机号'));
            } else {
                callback()
            }
        }, 100);
    } else {
        callback()
    }

};

const number = (rule, value, callback) => {
    if (value) {
        setTimeout(() => {
            let p = /^[1-9]\d{5}(18|19|20)\d{2}((0[1-9])|(1[0-2]))(([0-2][1-9])|10|20|30|31)\d{3}[0-9Xx]$/
            if (!p.test(value)) {
                callback(new Error('请输入18位身份证号'));
            } else {
                callback()
            }
        }, 100);
    } else {
        callback()
    }

};

const rules = reactive({
    phone: [
        { validator: phone, trigger: 'blur' }],
    number: [
        { validator: number, trigger: 'blur' }],
})

const onSubmit = () => {
    if (!change_form.value) return
    change_form.value.validate((valid) => {
        if (valid) {
            axios({
                url: 'user/edit_oneself/',
                params: {
                    username: localStorage.getItem('username')
                },
                data: form,
                method: 'POST'
            }).then(res => {
                if (res.data.code == 200) {
                    ElMessage({
                        message: "修改成功",
                        type: "success",
                    })
                    localStorage.setItem('username', res.data.new_username)
                    update()
                } else {
                    ElMessage({
                        message: "修改失败",
                        type: "error",
                    })
                }
            })
        } else {
            ElMessage({
                message: '信息填写有误',
                type: 'warning'
            })
        }
    })

}
</script>

<style scoped>
@import url('https://at.alicdn.com/t/c/font_4437435_sl83v9ltr19.css');

.container {
    width: 100%;
    min-height: 100vh;
    background: url("../assets/微信图片_20240419160325.jpg");
    background-size: cover;
    background-position: center;
    display: flex;
    justify-content: center;
    align-items: center;
}

.center {
    position: absolute;
    left: 50%;
    top: 50%;
    transform: translate(-50%, -50%);
    width: 500px;
    height: 700px;
    background-color: #fff;
    border-radius: 10px;
    box-shadow: 0 2px 12px 0 rgba(0, 0, 0, 0.1);
    padding: 20px;
    text-align: center
}

#Home_page {
    height: 50px;
    width: 100%;
    position: fixed;
    top: 0;
    background-color: rgba(110, 110, 110, 0.2);
    display: flex;
    justify-content: center;
    align-items: center;
    backdrop-filter: blur(15px);
}

#Home_page div {
    color: white;
    margin-left: 40px;
    line-height: 50px;
    position: absolute;
    left: 10px;
}

h3 {
    color: white;

}
</style>